package com.port.eleport.controller.plat;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.port.eleport.dto.base.Grid;
import com.port.eleport.dto.base.Json;
import com.port.eleport.dto.base.PageFilter;
import com.port.eleport.dto.plat.Merchant;
import com.port.eleport.dto.sys.Role;
import com.port.eleport.service.plat.MerchantServiceI;

@Controller
@RequestMapping("/merchant")
public class MerchantController {

	@Autowired
	private MerchantServiceI merchantService;

	@RequestMapping("/manager")
	public String manager() {
		return "/plat/merchant";
	}

	@RequestMapping("/dataGrid")
	@ResponseBody
	public Grid dataGrid(Merchant merchant, PageFilter ph) {
		Grid grid = new Grid();
		grid.setRows(merchantService.dataGrid(merchant, ph));
		grid.setTotal(merchantService.count(merchant, ph));
		return grid;
	}

	@RequestMapping("/addPage")
	public String addPage(HttpServletRequest request) {
		// request.setAttribute("stateList", GlobalConstant.statelist);
		return "/plat/merchantAdd";
	}

	@RequestMapping("/uploadLicense")
	@ResponseBody
	public Json uploadLicense(@RequestBody MultipartFile file,
			HttpServletRequest request) throws IOException {
		Json j = new Json();
		try {
			File dir = new File(System.getProperty("java.io.tmpdir"));
	
			if (dir.exists())
				dir.mkdir();
			String fileName = generateFileName();
			FileUtils.writeByteArrayToFile(new File(dir, fileName),
					file.getBytes());
	
			j.setMsg(fileName);
			j.setSuccess(true);
		}catch(Exception e){
			j.setMsg(e.getMessage());
		}
		
		return j;
	}

	@RequestMapping("/uploadIdCard")
	@ResponseBody
	public Json uploadIdCard(@RequestBody MultipartFile file,
			HttpServletRequest request) {
		Json j = new Json();
		try {
			File dir = new File(System.getProperty("java.io.tmpdir"));
	
			if (dir.exists())
				dir.mkdir();
			String fileName = generateFileName();
			FileUtils.writeByteArrayToFile(new File(dir, fileName),
					file.getBytes());
	
			j.setMsg(fileName);
			j.setSuccess(true);
		}catch(Exception e){
			j.setMsg(e.getMessage());
		}
		
		return j;
	}

	@RequestMapping("/editPage")
	public String editPage(HttpServletRequest request, String id) {
		Merchant m = merchantService.get(id);
		request.setAttribute("merchant", m);
		return "/plat/merchantEdit";
	}
	
	private String generateFileName() {
		// 获得当前时间
		DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		// 转换为字符串
		String formatDate = format.format(new Date());
		// 随机生成文件编号
		int random = new Random().nextInt(10000);
		return new StringBuffer().append(formatDate).append("_").append(random)
				.toString();
	}

}
